package com.skyz.base.manager;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.skyz.base.dialog.LoadingDialogFragment;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;

public class LoadingManager {
    private static final long LOADING_DISPLAY_MIN_TIME = 250;

    private final String TAG = getClass().getSimpleName();

    private final Handler mHandler = new Handler(Looper.getMainLooper());

    private LoadingManager() {
    }

    private static class InstanceHolder {
        private static final LoadingManager instance = new LoadingManager();
    }

    public static LoadingManager getInstance() {
        return InstanceHolder.instance;
    }

    private int mDialogCount = 0;
    private boolean mLoadingShow = false;

    private LoadingDialogFragment mLoadingDialogFragment = null;

    private final Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            dismissDialogImmediately();
        }
    };

    public void requestShowDialog() {
        if (!mLoadingShow) {
            //如果没有展示，把弹窗展示出来
            Activity activity = ActivityManager.getInstance().currentActivity();
            if (!(activity instanceof AppCompatActivity)) {
                Log.w(TAG, "appCompatActivity can show loading!");
                return;
            }
            final AppCompatActivity appCompatActivity = (AppCompatActivity) activity;
            appCompatActivity.getLifecycle().addObserver(new LifecycleEventObserver() {
                @Override
                public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
                    if (event == Lifecycle.Event.ON_DESTROY) {
                        if(mLoadingDialogFragment != null && mLoadingDialogFragment.isShowing()){
                            mLoadingDialogFragment.dismiss();
                        }
                        appCompatActivity.getLifecycle().removeObserver(this);
                    }
                }
            });
            mLoadingDialogFragment = LoadingDialogFragment.showDialogFragment(appCompatActivity.getSupportFragmentManager(), null);
            //标记为正在展示
            mLoadingShow = true;
        } else {
            //如果正在展示，并且在取消弹窗倒计时中，取消倒计时
            mHandler.removeCallbacks(mRunnable);
        }
        mDialogCount = mDialogCount++;
    }

    public void requestDismissDialog() {
        if (mDialogCount >= 1) {
            mDialogCount = mDialogCount--;
        }
        if (mDialogCount == 0) {
            mHandler.postDelayed(mRunnable, LOADING_DISPLAY_MIN_TIME);
        }
    }

    private void dismissDialogImmediately() {
        mHandler.removeCallbacks(mRunnable);
        if (!mLoadingShow) {
            return;
        }
        if (mLoadingDialogFragment == null) {
            return;
        }
        mLoadingDialogFragment.dismiss();
        mLoadingShow = false;
        mLoadingDialogFragment = null;
    }
}
